package com.quectel.leshan.client;

import com.eclipsesource.json.JsonObject;
import org.eclipse.leshan.client.californium.LeshanClient;
import org.eclipse.leshan.client.californium.LeshanClientBuilder;
import org.eclipse.leshan.client.object.Security;
import org.eclipse.leshan.client.object.Server;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.client.resource.SimpleInstanceEnabler;
import org.eclipse.leshan.core.LwM2mId;
import org.eclipse.leshan.core.model.InvalidDDFFileException;
import org.eclipse.leshan.core.model.InvalidModelException;
import org.eclipse.leshan.core.model.ObjectLoader;
import org.eclipse.leshan.core.model.StaticModel;
import org.eclipse.leshan.core.request.BindingMode;

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author robbin.zhao
 */
public class NbAepModuleSimulator {

    private static final String[] ddfpaths = new String[] { "LWM2M_Security-v1_0.xml", "LWM2M_Security-v1_1_1.xml",
            "LWM2M_Server-v1_0.xml", "LWM2M_Server-v1_1_1.xml", "LWM2M_Access_Control-v1_0_3.xml",
            /*"LWM2M_Device-v1_0_3.xml",*/ "LWM2M_Device-v1_1.xml",
            /*"LWM2M_Connectivity_Monitoring-v1_0_2.xml",*/ /*"LWM2M_Connectivity_Monitoring-v1_1.xml",*/
            /*"LWM2M_Connectivity_Monitoring-v1_2.xml",*/ /*"LWM2M_Firmware_Update-v1_0_3.xml",*/
            "LWM2M_Location-v1_0_2.xml", "LWM2M_Connectivity_Statistics-v1_0_4.xml",
            "LWM2M_OSCORE-v1_0_1.xml", "19.xml", "Connectivity_Monitoring-v1_1.xml"};

    public static void main(String[] args) throws InvalidModelException, InvalidDDFFileException, IOException {
        String endpoint = "123456123000007";
        LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);

        // create objects
        StaticModel model = new StaticModel(ObjectLoader.loadDdfResources("/models/", ddfpaths));
        ObjectsInitializer initializer = new ObjectsInitializer(model);
        initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://221.229.214.202:5683", 1000));
        initializer.setInstancesForObject(LwM2mId.SERVER, new Server(1000, 5 * 60, BindingMode.U, false));

        //添加设备（需继承自BaseInstanceEnabler）
        TestClientDevice testClientDevice = new TestClientDevice();
        initializer.setInstancesForObject(LwM2mId.DEVICE, testClientDevice);
        ConnectivityResource connectivityResource = new ConnectivityResource();
        initializer.setInstancesForObject(LwM2mId.CONNECTIVITY_MONITORING, connectivityResource);
        SimpleInstanceEnabler simpleInstanceEnabler = new SimpleInstanceEnabler();
        initializer.setInstancesForObject(LwM2mId.CONNECTIVITY_STATISTICS, simpleInstanceEnabler);
        DeviceDataEnabler deviceDataEnabler = new DeviceDataEnabler();
        initializer.setInstancesForObject(19, deviceDataEnabler);

        initializer.create(3, 4);
        // add it to the client
        builder.setObjects(initializer.createAll());

        LeshanClient client = builder.build();
        client.start();

        //创建服务停止时的钩子，销毁客户端（同时会通知服务端设备下线）
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                client.destroy(true); // send de-registration request before destroy
            }
        });

        subscribeCmd(deviceDataEnabler,testClientDevice,connectivityResource);

        /*while (true) {
            // 19/0/0
            //deviceDataEnabler.changeResource(0);

            // 3/0/9
            testClientDevice.changeBatteryLevel();
            try {
                Thread.sleep(1000 * 20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/

    }

    private static void subscribeCmd(DeviceDataEnabler deviceDataEnabler, TestClientDevice resource3, ConnectivityResource resource4) {
        try (Scanner scanner = new Scanner(System.in)) {
            while (scanner.hasNext()) {
                String command = scanner.next();
                switch (command) {
                    case "1":
                        deviceDataEnabler.changeResource(0);
                        break;
                    case "3":
                        resource3.changeBatteryLevel();
                        break;
                    case "4":
                        resource4.changeResource();
                        break;
                    default:
                        System.out.println("Unknown command !");
                }
            }
        }
    }
}
